/*
 =============================================================================
 FILE...............: ReceiptDAO.java
 COMMENTS...........: Class implemented by using the DAO (Data Access Object)
 					  design pattern for manipulating Receipt objects while
 					  accessing the database. This class extends the GenericDAO
 					  abstract class.
 =============================================================================
 AUTHOR(S)..........: Everton Ranielly de Sousa Cavalcante
 					  Department of Informatics and Applied Mathematics
 					  Federal University of Rio Grande do Norte
 LAST MODIFICATION..: September 17th, 2012
 ============================================================================= 					  
*/


/* ========================================================================= */
/* PACKAGES			                                                         */
/* ------------------------------------------------------------------------- */
package dao;


/* ========================================================================= */
/* IMPORTS			                                                         */
/* ------------------------------------------------------------------------- */
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Booking;
import model.Receipt;


/* ========================================================================= */
/* CLASS ReceiptDAO                                                          */
/* ------------------------------------------------------------------------- */
/**
 * Class implemented by using the DAO (Data Access Object) design pattern for 
 * manipulating <code>Receipt</code> objects while accessing the database. 
 * This class extends the <code>GenericDAO</code> abstract class.
 * @author Everton Cavalcante
 */
public class ReceiptDAO extends GenericDAO<Receipt, String>
{
	/** Default constructor */
	public ReceiptDAO()
	{ /* Empty */ }
	
	
	/**
	 * Inserts a receipt
	 * @param receipt Receipt to insert
	 */
	public void insert(Receipt receipt)
	{		
		try 
		{
			// Opens a connection 
			openConnection();
			
			// SQL query to be executed
			String query = "INSERT INTO receipts VALUES(?,?)";
			pstmt = conn.prepareStatement(query);
			
			// Data to be inserted
			pstmt.setString(1, receipt.getCode());
			pstmt.setString(2, receipt.getBooking().getBookingCode());
			
			// Executes query
			pstmt.execute();
		}
		catch (SQLException e) 
		{ 
			System.out.println("Error when inserting receipt:\n" + e.getMessage());
			System.exit(1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		finally
		{ closeConnection(); }
	}


	/**
	 * Updates a receipt
	 * @param receipt Receipt to update
	 * @param code Receipt code
	 */
	public void update(Receipt receipt, String code)
	{
		try 
		{
			// Opens a connection 
			openConnection();
			
			// SQL query to be executed
			String query = "UPDATE receipts SET receiptcode = ?, bookingcode = ? WHERE receiptcode = ?";
			pstmt = conn.prepareStatement(query);
			
			// Data to be updated
			pstmt.setString(1, receipt.getCode());
			pstmt.setString(2, receipt.getBooking().getBookingCode());
			pstmt.setString(3, code);
				
			// Executes query
			pstmt.execute();
		}
		catch (SQLException e) 
		{ 
			System.out.println("Error when updating receipt:\n" + e.getMessage());
			System.exit(1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		finally
		{ closeConnection(); }
	}
	
	
	/**
	 * Deletes a receipt
	 * @param code Receipt code
	 */
	public void delete(String code)
	{
		try 
		{
			// Opens a connection 
			openConnection();
			
			// SQL query to be executed
			String query = "DELETE FROM receipts WHERE receiptcode = ?";
			pstmt = conn.prepareStatement(query);
				
			// Data to be deleted
			pstmt.setString(1, code);
				
			// Executes query
			pstmt.execute();
		}
		catch (SQLException e) 
		{ 
			System.out.println("Error when deleting receipt:\n" + e.getMessage());
			System.exit(1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		finally
		{ closeConnection(); }
	}
	
	
	/**
	 * Returns the specified receipt
	 * @param code Receipt code
	 * @return Specified receipt
	 */
	public Receipt get(String code)
	{
		try 
		{
			// Opens a connection 
			openConnection();
				
			// SQL query to be executed
			String query = "SELECT * FROM receipts WHERE receiptcode = '" + code + "'";
			stmt = conn.prepareStatement(query);
				
			// Receipt to be returned
			Receipt result = null;
				
			// Executes query
			ResultSet rs = stmt.executeQuery(query);
			while (rs.next())
			{
				result = new Receipt();
				result.setCode(rs.getString("receiptcode"));
				
				BookingDAO bookingDAO = new BookingDAO();		// BookingDAO instance
				
				Booking booking = bookingDAO.get(rs.getString("bookingcode"));
				result.setBooking(booking);
			}
				
			// Returns the specified receipt
			if (result == null)
			{
				System.out.println("Error:\nReceipt not found");
				System.exit(1);
			}
			
			return result;
		}
		catch (SQLException e) 
		{ 
			System.out.println("Error when searching receipt:\n" + e.getMessage());
			System.exit(1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		finally
		{ closeConnection(); }
				
		return null;
	}
	
	
	/**
	 * Returns all registered receipts
	 * @return Registered receipts
	 */
	public List<Receipt> getAll()
	{
		try 
		{
			// Opens a connection 
			openConnection();
			
			// SQL query to be executed
			String query = "SELECT * FROM receipts";
			
			// List of receipts
			List<Receipt> receipts = new ArrayList<Receipt>();
			
			// Executes query
			ResultSet rs = stmt.executeQuery(query);
			while (rs.next())
			{
				// Current receipt
				Receipt receipt = new Receipt();
				receipt.setCode(rs.getString("receiptcode"));
				
				BookingDAO bookingDAO = new BookingDAO();		// BookingDAO instance
				
				Booking booking = bookingDAO.get(rs.getString("bookingcode"));
				receipt.setBooking(booking);
				
				// Adds the current airport
				receipts.add(receipt);
			}
			
			// Returns registered receipts
			if (receipts.isEmpty())
			{
				System.out.println("Error:\nNo receipts registered");
				System.exit(1);
			}
			
			return receipts;
		}
		catch (SQLException e) 
		{ 
			System.out.println("Error when searching receipts:\n" + e.getMessage());
			System.exit(1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		finally
		{ closeConnection(); }
		
		return null;
	}
}
